'''
Created on Aug 20, 2009

@author: echeek
'''

groups=[0,0,0];

def markGroups(x,y,value):
    coloff=x*9+(value-1);
    rowoff=y*9+(value-1);
    boxoff=(x/3+3*int(y/3))*9+(value-1);
    groups[0]|=1<<coloff;
    groups[1]|=1<<rowoff;
    groups[2]|=1<<boxoff;
    
def unmarkGroups(x,y,value):
    coloff=x*9+(value-1);
    rowoff=y*9+(value-1);
    boxoff=(x/3+3*int(y/3))*9+(value-1);
    groups[0]^=1<<coloff;
    groups[1]^=1<<rowoff;
    groups[2]^=1<<boxoff;
    
    
def checkOpenGroups(x,y,value):
    coloff=x*9+(value-1);
    rowoff=y*9+(value-1);
    boxoff=(x/3+3*int(y/3))*9+(value-1);
    return groups[0]&(1<<coloff)==0 and groups[1]&(1<<rowoff)==0 and groups[2]&(1<<boxoff)==0;

def resetGroups():
    groups[0]=0;
    groups[1]=0;
    groups[2]=0; 
    

def solveBoard(board):
    resetGroups();
    visited=[];
    pos=0;
    
    for i in range(9):
        for j in range(9):
            if(board[j][i]!=0):
                markGroups(i,j,board[j][i]);
    
    undo=False;
    while pos<81:
        x=pos%9;
        y=pos/9;
        if( not undo and board[y][x]!=0):
            pos+=1;
            continue;
        
        i=1;
        if undo:
            i=board[y][x]+1;
            unmarkGroups(x,y,board[y][x]);
            
        undo=True;
        while i<=9:
            if checkOpenGroups(x,y,i):
                board[y][x]=i;
                markGroups(x,y,i);
                undo=False;
                break;
            i+=1;
        
        if undo:
            board[y][x]=0;
            pos=visited.pop();
        else:
            visited.append(pos);
            pos+=1;
            
    return board;
    
def getContribution(board):
    return 100*board[0][0]+10*board[0][1]+board[0][2];

def lineToArray(line):
    arr=[];
    for x in line:
        arr.append(int(x));
    return arr;


def Euler96():
    total=0;
    data=open('Euler96data.txt', 'r');
    board=[];
    
    
    while True:
        line=data.readline()[:-1];
        if(line is None or line==''):
            break;
        if(len(line)<3):
            continue;
        board.append(lineToArray(line));
        if(len(board)==9):
            board=solveBoard(board);
            total+=getContribution(board);
            print board;
            board=[];
    print total;
    
Euler96();